package com.example.demo.service.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class EmailService {
    @Autowired
    private JavaMailSender javaMailSender;
 
    @Value("${spring.mail.username}")
    private String sender;
 
    public synchronized void sendSimpleEmail(String email, String title, String text) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(sender);
        mailMessage.setTo(email);
        mailMessage.setSubject(title);
        mailMessage.setText(text);
        javaMailSender.send(mailMessage);
    }
    
    /**
     * 定时监控CPU和内存使用率（异常则发送邮件）
     */
//    @Scheduled(cron = "0 */1 * * * ?")
//    public void monitorCPU() {
//        logger.info("定时监控CPU和内存使用率任务开启");
//        float cpuUsage = OSUtils.cpuUsage();
//        logger.info("cpuUsage:" + cpuUsage);
//        if (cpuUsage > 70.0) {
//            emailService.sendSimpleEmail("583509857@qq.com", "服务器cpu使用率过高，请注意查看", "拳秀体育：服务器提醒:" + wechatTransfer.getIp());
//        }
//        long memoryUsage = OSUtils.memoryUsage();
//        logger.info("memoryUsage:" + memoryUsage);
//        if ((memoryUsage / 1024) < 100) {
//            emailService.sendSimpleEmail("583509857@qq.com", "服务器内存剩余空间不足，请注意查看", "拳秀体育：服务器提醒:" + wechatTransfer.getIp());
//        }
//    }
 
}
//原文链接：https://blog.csdn.net/fy_java1995/java/article/details/95767360
